<template>
	<el-dialog title="审核充值订单" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="120px" label-position="left">
			<el-form-item label="订单信息" prop="ordernum">
				<el-input v-model="form.ordernum" disabled></el-input>
			</el-form-item>
			<el-form-item label="会员ID" prop="member_id">
				<el-input v-model="form.member_id" disabled></el-input>
			</el-form-item>
			<el-form-item label="充值金额" prop="amount">
				<el-input v-model="form.amount" disabled></el-input>
			</el-form-item>
			<el-form-item label="审核结果" prop="audit_status">
				<el-radio-group v-model="form.audit_status">
					<el-radio label="1">审核通过</el-radio>
					<el-radio label="4">审核驳回</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="第三方单号" prop="tordernum" v-if="form.audit_status == '1'">
				<el-input v-model="form.tordernum" clearable placeholder="请输入第三方单号"></el-input>
			</el-form-item>
			<el-form-item label="驳回原因" prop="remark" v-if="form.audit_status == '4'">
				<el-input v-model="form.remark" type="textarea" :rows="3" clearable placeholder="请输入驳回原因"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id: '',
					ordernum: '',
					member_id: '',
					amount: '',
					audit_status: '1',
					tordernum: '',
					remark: ''
				},
				//验证规则
				rules: {
					audit_status: [
						{required: true, message: '请选择审核结果', trigger: 'change'}
					],
					tordernum: [
						{required: true, message: '请输入第三方单号', trigger: 'blur'}
					],
					remark: [
						{required: true, message: '请输入驳回原因', trigger: 'blur'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(){
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						// 动态验证
						if (this.form.audit_status == '1' && !this.form.tordernum) {
							this.$alert('审核通过时必须填写第三方单号', '提示', {type: 'warning'});
							return;
						}
						if (this.form.audit_status == '4' && !this.form.remark) {
							this.$alert('审核驳回时必须填写驳回原因', '提示', {type: 'warning'});
							return;
						}

						this.isSaveing = true;
						var reqData = {
							id: this.form.id,
							status: this.form.audit_status,
							tordernum: this.form.audit_status == '1' ? this.form.tordernum : '',
							remark: this.form.audit_status == '4' ? this.form.remark : ''
						};
						
						console.log('提交数据:', reqData); // 调试信息
						
						var res = await this.$API.rechargeOrder.audit.post(reqData);
						this.isSaveing = false;
						
						if(res.code == 200){
							this.$emit('success', this.form, 'audit')
							this.visible = false;
							this.$message.success("审核成功")
						}else{
							this.$alert(res.msg, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.ordernum = data.ordernum
				this.form.member_id = data.member_id
				this.form.amount = data.amount
				this.form.audit_status = '1'
				this.form.tordernum = ''
				this.form.remark = ''
			}
		}
	}
</script>

<style>
</style>
